//
//  XJChatMainViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/12.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJChatMainViewController.h"
//controller
#import "XJSystemMSgViewController.h" //1、系统消息
#import "XJOrderMSgViewController.h" // 2.订单消息
#import "XJCommentMSgViewController.h" // 3.评论消息

//view
#import "XJChatMainTableViewCell.h"

@interface XJChatMainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView * xjcmTableView;
@property (nonatomic, strong) NSArray * xjcmArray;

@end

@implementation XJChatMainViewController

- (UITableView *)xjcmTableView{
    if (!_xjcmTableView) {
        UITableView * xjcmTableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        self.xjcmTableView = xjcmTableView;
        [self.view addSubview:xjcmTableView];
    }
    return _xjcmTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navTitle = @"消息";
    [self initSubView];
}

- (void)initSubView{
    
    self.xjcmTableView.backgroundColor                  = WHITE_COLOR;
    self.xjcmTableView.showsVerticalScrollIndicator     = NO;
    self.xjcmTableView.showsHorizontalScrollIndicator   = NO;
    
    self.xjcmTableView.delegate        =   self;
    self.xjcmTableView.dataSource      =   self;
    [self setExtraCellLineHidden:self.xjcmTableView];
    
}

- (NSArray *)xjcmArray{
    if (!_xjcmArray) {
        _xjcmArray = @[
                        @[@"系统消息",@"mes_xitong"],
                        @[@"订单消息",@"mes_order"],
                        @[@"评论消息",@"mes_comment"],
                        ];
    }
    return _xjcmArray;
    
}

#pragma mark tableView datasource && delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return self.xjcmArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        XJChatMainTableViewCell * xjcmCell = [XJChatMainTableViewCell cellWithTableView:tableView];
        xjcmCell.selectionStyle      = UITableViewCellSelectionStyleGray;
        NSArray *rows                = self.xjcmArray[indexPath.row];
        xjcmCell.xjChatImgV.image = [UIImage imageNamed:[rows lastObject]];
        xjcmCell.xjChatLab.text   = [rows firstObject];
        return xjcmCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    return ADAPTHeight(60);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        NSArray *array   = self.xjcmArray[indexPath.row];
        
        NSString *item   = [array lastObject];
        
        SEL cellSelector = NSSelectorFromString(item);
        
        if ([self respondsToSelector:cellSelector]) {
            ((void (*)(id, SEL))objc_msgSend)(self,cellSelector);
        }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(10, 0, KWidth-20, 0.3)];
    
    UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, KWidth-20, 0.3)];
    [view addSubview:backView];
    backView.backgroundColor = XJCOLOR_RGB(206, 211, 213);
    return view;
}

//设置分割线
- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
        if (indexPath.row < self.xjcmArray.count) {
            //四个参数：上左下右
            cell.separatorInset=UIEdgeInsetsMake(0, 10, 0, 10);
        }
    }
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

//隐藏多余分割线
-(void)setExtraCellLineHidden: (UITableView *)tableView{
    
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

- (void)mes_xitong{
    
    XJLog(@"点击系统消息");
    
    XJSystemMSgViewController *systemMsgVC = [[XJSystemMSgViewController alloc] init];
    [self.navigationController pushViewController:systemMsgVC animated:YES];
    
}

- (void)mes_order{
    
    XJLog(@"点击订单消息");
    XJOrderMSgViewController *orderMsgVC = [[XJOrderMSgViewController alloc] init];
    [self.navigationController pushViewController:orderMsgVC animated:YES];
}

- (void)mes_comment{
    
    XJLog(@"点击评论消息");
    XJCommentMSgViewController *commentMsgVC = [[XJCommentMSgViewController alloc] init];
    [self.navigationController pushViewController:commentMsgVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
